package cn.wx.read.csv;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import com.csvreader.CsvReader;
import cn.wx.common.utils.JSONUtils;
import cn.wx.common.utils.UUIDGenerator;

public class ReadData {
	
	private static Map<String,Long> map = new HashMap<>();
	
	public static void main(String[] args) throws Exception {
		
		
		File file = new File("E:\\my_read_csv\\wfdata\\ms_expert_info.csv");
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		int ii = 0;
		while (csvReader.readRecord()){
			// 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取专家" + (ii++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}
			String nId = csvReader.get(0);
			String nId_old = csvReader.get(1);
			map.put(nId_old, Long.valueOf(nId));
			
		}
		
		/**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
		
		
		//readBook("126838900", "Radiology","expert放射.csv");
		
		//readPaper("126838900", "Radiology","zyyt-wf-20190103\\paper放射.csv");
		//readPaper("118487528", "Ophthalmology","zyyt-wf-20190103\\paper眼科.csv");
		
		//readPatent("126838900", "Radiology","zyyt-wf-20190103\\patent放射.csv");
		//readPatent("118487528", "Ophthalmology","zyyt-wf-20190103\\patent眼科.csv");
		
		//readExpertFiled("126838900", "Radiology","expert放射.csv");
		//readExpertFiled("118487528", "Ophthalmology","expert眼科.csv");
		
		readExpertGHIndex("expert_index.csv");
		
	}
	
	
	/**
	 * 替换hindex gindex
	 * @throws IOException 
	 * 
	 */
	public static void readExpertGHIndex(String dcsvName) throws IOException {
		
		StringBuffer sb = null;
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\expert_index_th.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);
		
		//"nId","hIndex","gindex"
		
		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		.append(",").append(JSONUtils.c("hIndex"))
		.append(",").append(JSONUtils.c("gindex"));
		
		output.write(sb.toString());
		output.newLine();
		
		File file = new File("E:\\my_read_csv\\wfdata\\" + dcsvName);
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		int ii = 0;
		while (csvReader.readRecord()){
			// 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取专家" + (ii++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}
			String nId_old = csvReader.get(0);
			String hIndex = csvReader.get(1);
			String gindex = csvReader.get(2);

			Long nId = map.get(nId_old);
			
			if(nId == null) {
				continue;
			}
			
			/**
			 * 专家和领域的关系
			 */
			sb = new StringBuffer();
			sb.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(hIndex)).append(",")
			.append(JSONUtils.c(gindex));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
		}
		
		/**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
		
		output.flush();
        output.close();
        output = null;
		
	}
	
	
	
	/**
	 * 整理专家对应的领域
	 * @throws IOException
	 */
	public static void readExpertFiled(String fieldId,String fieldName,String dcsvName) throws IOException {
		
		StringBuffer sb = null;
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\expertFieldRelation.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		sb = new StringBuffer();
		sb.append(JSONUtils.c("fosId"))
		.append(",").append(JSONUtils.c("expertId"));
		
		output.write(sb.toString());
		output.newLine();
		
		
		
		File ofR = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\expertFosName.csv");
		FileOutputStream ofsR = new FileOutputStream(ofR,false);
		OutputStreamWriter ofwR = new OutputStreamWriter(ofsR,"utf-8");
		BufferedWriter outputR = new BufferedWriter(ofwR);

		sb = new StringBuffer();
		sb.append(JSONUtils.c("expertId"))
		.append(",").append(JSONUtils.c("fosName"));
		
		outputR.write(sb.toString());
		outputR.newLine();
		
		
		
		File file = new File("E:\\my_read_csv\\wfdata\\zyyt-wf-20190102\\" + dcsvName);
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		int ii = 0;
		while (csvReader.readRecord()){
			// 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取领域专家" + (ii++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}
			String nId_old = csvReader.get(0);
			
			Long nId = map.get(nId_old);
			
			/**
			 * 专家和领域的关系
			 */
			sb = new StringBuffer();
			sb.append(JSONUtils.c(fieldId)).append(",")
			.append(JSONUtils.c(nId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
			/**
			 * 专家和领域名称
			 */
			sb = new StringBuffer();
			sb.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(fieldName));
			outputR.write(sb.toString());
			outputR.newLine();
			outputR.flush();
		}
		
		/**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
		
		output.flush();
        output.close();
        output = null;
	}
	
	
	public static void readPatent(String fieldId,String fieldName,String dcsvName) throws IOException {
		
		StringBuffer sb = null;
		
		File file = new File("E:\\my_read_csv\\wfdata\\" + dcsvName);
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\ms_expert_patent.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		//"nId","title","cit","year","url","fAbstract","Authors","AuthorsId"
		
		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		.append(",").append(JSONUtils.c("title"))
		.append(",").append(JSONUtils.c("cit"))
		.append(",").append(JSONUtils.c("year"))
		.append(",").append(JSONUtils.c("url"))
		.append(",").append(JSONUtils.c("fAbstract"))
		.append(",").append(JSONUtils.c("Authors"))
		.append(",").append(JSONUtils.c("AuthorsId"));
		
		output.write(sb.toString());
		output.newLine();
		
		
		
		File ofR = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\expertPatentRelation.csv");
		FileOutputStream ofsR = new FileOutputStream(ofR,false);
		OutputStreamWriter ofwR = new OutputStreamWriter(ofsR,"utf-8");
		BufferedWriter outputR = new BufferedWriter(ofwR);

		sb = new StringBuffer();
		sb.append(JSONUtils.c("expertId"))
		.append(",").append(JSONUtils.c("patentId"));
		
		outputR.write(sb.toString());
		outputR.newLine();
		
		
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取专利" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","title","cit","year","url","fAbstract","Authors","AuthorsId"
			String nId = csvReader.get(0);
			String title = csvReader.get(1);
			String cit = csvReader.get(2);
			String year = csvReader.get(3);
			String url = csvReader.get(4);
			String fAbstract = csvReader.get(5);
			String Authors = csvReader.get(6);
			String AuthorsId = csvReader.get(7);
			

			final String patentId = UUIDGenerator.getRanLongUUID();
			sb = new StringBuffer();
			sb.append(JSONUtils.c(patentId)).append(",")
			//.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(title)).append(",")
			.append(JSONUtils.c(cit)).append(",")
			.append(JSONUtils.c(year)).append(",")
			.append(JSONUtils.c(url)).append(",")
			.append(JSONUtils.c(fAbstract)).append(",")
			.append(JSONUtils.c(Authors)).append(",")
			.append(JSONUtils.c(AuthorsId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
			/**
			 * 保存专家和图书的关系
			 */
			
			if(StringUtils.isNotBlank(AuthorsId)) {
				
				String[] df = AuthorsId.split(",");
				
				for(String kj : df) {
					Long expertId = map.get(kj);
					
					if(expertId == null) {
						continue;
					}
					
					sb = new StringBuffer();
					sb.append(JSONUtils.c(expertId)).append(",")
					.append(JSONUtils.c(patentId));
					outputR.write(sb.toString());
					outputR.newLine();
				}
				outputR.flush();
			}
			
        }
		
        output.flush();
        output.close();
        output = null;
        
        outputR.flush();
        outputR.close();
        outputR = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
	}
	
	
	public static void readPaper(String fieldId,String fieldName,String dcsvName) throws IOException {
		
		StringBuffer sb = null;
		
		File file = new File("E:\\my_read_csv\\wfdata\\" + dcsvName);
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\ms_expert_paper.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		//"nId","title","citations","year","url","fAbstract","publisher","Authors","docType","arrayFos","AuthorsId"
		
		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		//.append(",").append(JSONUtils.c("nId_old"))
		.append(",").append(JSONUtils.c("title"))
		.append(",").append(JSONUtils.c("citations"))
		.append(",").append(JSONUtils.c("year"))
		.append(",").append(JSONUtils.c("url"))
		.append(",").append(JSONUtils.c("fAbstract"))
		.append(",").append(JSONUtils.c("publisher"))
		.append(",").append(JSONUtils.c("Authors"))
		.append(",").append(JSONUtils.c("docType"))
		.append(",").append(JSONUtils.c("arrayFos"))
		.append(",").append(JSONUtils.c("AuthorsId"));
		
		output.write(sb.toString());
		output.newLine();
		
		
		
		File ofR = new File("E:\\my_read_csv\\wfdata\\"+ fieldName +"\\expertPaperRelation.csv");
		FileOutputStream ofsR = new FileOutputStream(ofR,false);
		OutputStreamWriter ofwR = new OutputStreamWriter(ofsR,"utf-8");
		BufferedWriter outputR = new BufferedWriter(ofwR);

		sb = new StringBuffer();
		sb.append(JSONUtils.c("expertId"))
		.append(",").append(JSONUtils.c("paperId"));
		
		outputR.write(sb.toString());
		outputR.newLine();
		
		
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取论文" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","title","citations","year","url","fAbstract","publisher","Authors","docType","arrayFos","AuthorsId"
			String nId = csvReader.get(0);
			String title = csvReader.get(1);
			String citations = csvReader.get(2);
			String year = csvReader.get(3);
			String url = csvReader.get(4);
			String fAbstract = csvReader.get(5);
			String publisher = csvReader.get(6);
			String Authors = csvReader.get(7);
			String docType = csvReader.get(8);
			String arrayFos = csvReader.get(9);
			String AuthorsId = csvReader.get(10);
			
			arrayFos = fieldName;
			
			final String paperId = UUIDGenerator.getRanLongUUID();
			sb = new StringBuffer();
			sb.append(JSONUtils.c(paperId)).append(",")
			//.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(title)).append(",")
			.append(JSONUtils.c(citations)).append(",")
			.append(JSONUtils.c(year)).append(",")
			.append(JSONUtils.c(url)).append(",")
			.append(JSONUtils.c(fAbstract)).append(",")
			.append(JSONUtils.c(publisher)).append(",")
			.append(JSONUtils.c(Authors)).append(",")
			.append(JSONUtils.c(docType)).append(",")
			.append(JSONUtils.c(arrayFos)).append(",")
			.append(JSONUtils.c(AuthorsId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
			/**
			 * 保存专家和图书的关系
			 */
			
			if(StringUtils.isNotBlank(AuthorsId)) {
				
				String[] df = AuthorsId.split(",");
				
				for(String kj : df) {
					Long expertId = map.get(kj);
					
					if(expertId == null) {
						continue;
					}
					
					sb = new StringBuffer();
					sb.append(JSONUtils.c(expertId)).append(",")
					.append(JSONUtils.c(paperId));
					outputR.write(sb.toString());
					outputR.newLine();
				}
				outputR.flush();
			}
			
        }
		
        output.flush();
        output.close();
        output = null;
        
        outputR.flush();
        outputR.close();
        outputR = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
	}
	
	
	public static void readBook() throws IOException {
		
		StringBuffer sb = null;
		
		File file = new File("E:\\my_read_csv\\wfdata\\book.csv");
        FileInputStream fileInputStream1 = new FileInputStream(file);  
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(fileInputStream1);
		
        /**
         * 将专家的新id 和 旧id 读入map
         */
		CsvReader csvReader = new CsvReader(bufferedInputStream1,Charset.forName("utf-8"));
		csvReader.readHeaders();
		
		BufferedWriter output = null;
		File of = new File("E:\\my_read_csv\\wfdata\\ms_expert_book.csv");
		FileOutputStream ofs = new FileOutputStream(of,false);
		OutputStreamWriter ofw = new OutputStreamWriter(ofs,"utf-8");
		output = new BufferedWriter(ofw);

		//"nId","title","publisher","year","Authors","AuthorsId"
		
		sb = new StringBuffer();
		sb.append(JSONUtils.c("nId"))
		//.append(",").append(JSONUtils.c("nId_old"))
		.append(",").append(JSONUtils.c("title"))
		.append(",").append(JSONUtils.c("publisher"))
		.append(",").append(JSONUtils.c("year"))
		.append(",").append(JSONUtils.c("Authors"))
		.append(",").append(JSONUtils.c("AuthorsId"));
		
		output.write(sb.toString());
		output.newLine();
		
		
		
		File ofR = new File("E:\\my_read_csv\\wfdata\\expertBookRelation.csv");
		FileOutputStream ofsR = new FileOutputStream(ofR,false);
		OutputStreamWriter ofwR = new OutputStreamWriter(ofsR,"utf-8");
		BufferedWriter outputR = new BufferedWriter(ofwR);

		//"nId","title","publisher","year","Authors","AuthorsId"
		
		sb = new StringBuffer();
		sb.append(JSONUtils.c("expertId"))
		.append(",").append(JSONUtils.c("bookId"));
		
		outputR.write(sb.toString());
		outputR.newLine();
		
		
		int i = 0;
        while (csvReader.readRecord()){
        	
            // 读一整行
        	String line = csvReader.getRawRecord();
			System.out.println("读取" + (i++) +"行");
			if(StringUtils.isBlank(line)) {
				continue;
			}

			//"nId","title","publisher","year","Authors","AuthorsId"
			String nId = csvReader.get(0);
			String title = csvReader.get(1);
			String publisher = csvReader.get(2);
			String year = csvReader.get(3);
			String Authors = csvReader.get(4);
			String AuthorsId = csvReader.get(5);

			final String bookId = UUIDGenerator.getRanLongUUID();
			sb = new StringBuffer();
			sb.append(JSONUtils.c(bookId)).append(",")
			//.append(JSONUtils.c(nId)).append(",")
			.append(JSONUtils.c(title)).append(",")
			.append(JSONUtils.c(publisher)).append(",")
			.append(JSONUtils.c(year)).append(",")
			.append(JSONUtils.c(Authors)).append(",")
			.append(JSONUtils.c(AuthorsId));
			
			output.write(sb.toString());
			output.newLine();
			output.flush();
			
			/**
			 * 保存专家和图书的关系
			 */
			Long expertId = map.get(AuthorsId);
			
			sb = new StringBuffer();
			sb.append(JSONUtils.c(expertId)).append(",")
			.append(JSONUtils.c(bookId));
			
			outputR.write(sb.toString());
			outputR.newLine();
			outputR.flush();
			
        }
		
        output.flush();
        output.close();
        output = null;
        
        outputR.flush();
        outputR.close();
        outputR = null;
        
        /**
		 * 关闭读入流
		 */
		csvReader.close();
		csvReader = null;
	}
	
	
}
